﻿function copyContent(obj) {
    var oInput = document.createElement('input');
    oInput.value = obj;
    document.body.appendChild(oInput);
    oInput.select(); // 选择对象
    document.execCommand("Copy"); // 执行浏览器复制命令
    oInput.className = 'oInput';
    oInput.style.display = 'none';
    BUI.Message.Alert('复制分享链接成功', 'success');
}

function backRoot(url) {
    self.location = url;
}

//判断字符是否为空的方法
function isEmptyStr(obj) {
    if (typeof obj === "undefined" || obj === null || obj === "") {
        return true;
    } else {
        return false;
    }
}

function showSucc(obj, url, reloadHandler) {
    BUI.Message.Alert(obj, function () {
        if (!isEmptyStr(url))
            location.replace(url);
        else if (reloadHandler !== undefined)
            reloadHandler();
    }, 'success');
}
function showFail(obj, url, reloadHandler) {
    if ($("#spMsg").length > 0) {
        $("#spMsg").html(obj);
        return;
    } else {
        BUI.Message.Alert(obj, function () {
            if (!isEmptyStr(url))
                location.href = url;
            else if (reloadHandler !== undefined)
                reloadHandler();
        }, 'error');
    }
}

function showimg(url) {
    window.open(url);
}

function deleteParentElement(Obj) {
    Obj.parentNode.parentNode.removeChild(Obj.parentNode);
}

function handleFiles(files, inputFileSrc) {
    for (var i = 0; i < files.length; i++) {
        var file = files[i];
        var imageType = /image.*/;

        if (!file.type.match(imageType)) {
            DoAlert("请选择图片");
            continue;
        }
        var img = document.getElementById(inputFileSrc);
        var reader = new FileReader();
        reader.onload = function (e) {
            img.src = e.target.result;
            $("#" + inputFileSrc + "_btn").hide();
        }
        reader.readAsDataURL(file);
    }
}
function handleFilesZip(files, inputFileSrc) {
    for (var i = 0; i < files.length; i++) {
        var file = files[i];

        var size = (file.size / 1024.0 / 1024.0);
        $("#" + inputFileSrc).text(size.toFixed(2) + "M");

        if (size > 100) {
            alert("上传的文件不要超过100M");
            return;
        }
        //var reader = new FileReader();
        //reader.onload = function (e) {
        //$("#" + inputFileSrc).text("fadfa" + e.target.result);            
        //}
        //reader.readAsDataURL(file);
    }
}

function ShowActionError(result) {
    var substr = cutstr(result, "BUI.Message.Alert", "function");
    substr = substr.replace("'", "").replace("'", "").replace("(", "").replace(",", "");
    if (substr == "")
        BUI.Message.Alert("操作失败，请联系技术人员", 'warning');
    else {
        if (substr.indexOf("请重新登录") > 0) {
            BUI.Message.Alert(substr, function () {
                top.location.href = "/User/Login";
            }, 'warning');
        }
        else {
            {
                if (substr.indexOf("请重新登录") > 0) {
                    BUI.Message.Alert(substr, function () {
                        top.location.href = "/User/Login";
                    }, 'warning');
                }
                else {
                    BUI.Message.Alert(substr, 'warning');
                }
            }
        }
    }
}

///提示提交的结果
function ShowActionResult(result, reloadHandler) {
    if (result.Message != undefined) {
        if (result.IsSuccess) {
            showSucc(result.Message, result.RedirectUrl, reloadHandler);
        }
        else {
            showFail(result.Message, "");
        }
    }
    else {
        ShowActionError(result);
    }
}
///提示提交的结果，错误也要进行Handler
function ShowActionResultForAll(result, reloadHandler, failHandler) {
    if (result.Message != undefined) {
        if (result.IsSuccess) {
            showSucc(result.Message, result.RedirectUrl, reloadHandler);
        }
        else {
            showFail(result.Message, "", failHandler);
        }
    }
    else {
        ShowActionError(result);
    }
}
function bindImgBtn() {
    BUI.use('bui/overlay', function (Overlay) {
        $('.thumb-img').on('click', function () {
            var dialog = new Overlay.Dialog({
                title: '显示大图',
                width: 800,
                height: 600,
                buttons: [],
                bodyContent: '<p><img style="height:470px" src="' + $(this).attr("src").replace("?x-oss-process=style/small", "") + '"> </p>'
            });
            dialog.show();
        });

        $('.list-img').on('click', function (e) {
            e.preventDefault();
            var href = $(this).attr("href").replace("?x-oss-process=style/small", "");
            var dialog = new Overlay.Dialog({
                title: '显示大图',
                width: 800,
                height: 600,
                buttons: [],
                bodyContent: '<p><img style="height:470px" src="' + href + '"> </p>'
            });
            dialog.show();
        });

    });
}

function goback() {
    var backbtn = $("#backid");
    if (backbtn) {
        var backpageid = backbtn.attr("backpageid");
        if (backpageid) {
            var backurl = BUI.Cookie.get(backpageid);
            if (backurl) {
                self.location = backurl;
                return;
            }
        }
    }
    if (document.referrer) {
        if (document.referrer == self.location) {
            history.back(-1);
        } else {
            //history.back(-1);
            //location.reload();
            self.location = document.referrer;
        }
    } else {
        if (backbtn) {
            var link = backbtn.attr("href");
            if (typeof (link) == undefined || link == "" || link == "undefined" || link == undefined) {
                parent.location.reload();
            } else {
                self.location = link;
            }
        }
    }
}

$(document).ready(function () {
    bindImgBtn();

    //打开连接
    $(".opennewtab").click(function (e) {
        e.preventDefault();
        var href = $(this).attr("href");
        if (top.topManager) {
            top.topManager.openPage({
                href: href
            });
        }
    });

    //打开内容
    $(".openbasetab").click(function (e) {
        e.preventDefault();
        var href = $(this).attr("href");
        if (top.topManager) {
            top.topManager.openPage({
                href: href
            });
        }
    });

    $("#backid").click(function () {

        var backpageid = $(this).attr("backpageid");
        if (backpageid) {
            var backurl = BUI.Cookie.get(backpageid);
            if (backurl) {
                self.location = backurl;
                return;
            }
        }
        if (document.referrer) {
            if (document.referrer == self.location) {
                history.back(-1);
            } else {
                //history.back(-1);
                //location.reload();
                self.location = document.referrer;
            }
        } else {
            var link = $(this).attr("href");
            if (typeof (link) == undefined || link == "" || link == "undefined" || link == undefined) {
                parent.location.reload();
            } else {
                self.location = link;
            }
        }
    });

    $('.del_img_btn').click(function () {
        deleteParentElement(this);
    });

    //全选
    $('#chkall').on('click', function () {
        if ($('#chkall').attr("checked") == "checked") {
            $("input[type=checkbox][name=cbx_last]").each(function () {
                //设置选中
                $(this).attr("checked", "checked");
            });
        }
        else {
            $("input[type=checkbox][name=cbx_last]").each(function () {
                //移除选中
                $(this).removeAttr("checked");
            });
        }
    });

    BUI.use(['bui/form', 'bui/overlay'], function (Form, overlay) {
        new Form.Form({
            srcNode: '#S_Form'
        }).render();

        new Form.Form({
            srcNode: '.S_Form'
        }).render();

        //判断手机号格式
        jQuery.validator.addMethod("isMobile", function (value, element) {
            var length = value.length;
            //var mobile = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/;
            var mobile = /^((1{1})+\d{10})$/;
            return this.optional(element) || (mobile.test(value));
        }, "请正确填写您的手机号码");

        //判断中文字符
        jQuery.validator.addMethod("isChinese", function (value, element) {
            return this.optional(element) || /^[\u0391-\uFFE5]+$/.test(value);
        }, "只能包含中文字符");

        //判断只能字母或数字
        jQuery.validator.addMethod("IsNumOrLetter", function (value, element) {
            return this.optional(element) || /^(\d|[a-zA-Z])+$/.test(value);
        }, "只能字母或数字的组合");

        //判断只能字母,数字,.符号,_下划线或-杠
        jQuery.validator.addMethod("IsPwd", function (value, element) {
            return this.optional(element) || /^(\d|[a-zA-Z\_])+$/.test(value);
        }, "只能字母,数字,.符号,_下划线或-杠的组合");
        //判断只能字母或者#号
        jQuery.validator.addMethod("IsLetterOrHashtag", function (value, element) {
            return this.optional(element) || /^(#)|([a-zA-Z]+)$/.test(value);
        }, "只能是字母或“#”");

        //判断只能数值
        jQuery.validator.addMethod("IsNumber", function (value, element) {
            return this.optional(element) || /^([1-9]\d{0,9}|0)([.]?|(\.\d{1,6})?)$/.test(value);
        }, "只能是数值");
        //判断只能大于0数值
        jQuery.validator.addMethod("IsBigThan0", function (value, element) {
            return this.optional(element) || (/^([1-9]\d{0,9}|0)([.]?|(\.\d{1,6})?)$/.test(value) && (value > 0));
        }, "必须是大于0的数值");
    });

    $("input").attr("autocomplete", "off");


    //设置页面信息
    var pageid = $("#pageid").val();
    if (pageid) {
        var nowurl = document.location.href;
        BUI.Cookie.set(pageid, nowurl, "", "", "/");
    }

    //if (document.referrer) {
    //    if (document.referrer.toLowerCase().indexOf("/home/index") >= 0) {
    //        $("#backid").hide();
    //    }
    //} else {
    //    $("#backid").hide();
    //}
});



//获取checkbox列表的选中项function getCheckboxList() {    var finIds = "";
    //遍历角色checkbox
    $("input[type=checkbox][name=cbx_last]").each(function () {
        if ($(this).attr("checked") == "checked") {
            finIds += $(this).val() + ',';
        }
    });
    return finIds;
}
